package com.lovcreate.utils;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 解析后缀名为 xlsx 的文件
 * Created by Administrator on 2016/7/26.
 */
public class XlsxParser implements ExcelParser {
    @Override
    public List<Map<String,String>> excelParser(String filePath) {
        List<Map<String,String>> list = new ArrayList<Map<String,String>>();
        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
            XSSFWorkbook XSSFWorkbook = new XSSFWorkbook(is);

            // 循环工作表 Sheet
            for(int numSheet = 0; numSheet < XSSFWorkbook.getNumberOfSheets(); numSheet++) {

                XSSFSheet xssfSheet = XSSFWorkbook.getSheetAt(numSheet);
                if (xssfSheet.getLastRowNum() == 0) {
                    continue;
                }

                List<String> fieldList = new ArrayList<String>();       // 字段名
                for(int colNum = xssfSheet.getRow(0).getFirstCellNum(); colNum < xssfSheet.getRow(0).getLastCellNum();colNum++) {
                    String fieldName = xssfSheet.getRow(0).getCell(colNum).getStringCellValue();
                    fieldList.add(fieldName);
                }

                // 循环行Row, 第一行为表的字段名
                for (int rowNum = xssfSheet.getFirstRowNum() + 1; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
                    // 监视 rowNum
                    System.out.println(rowNum);
                    XSSFRow xssfRow = xssfSheet.getRow(rowNum);
                    if(xssfRow != null) {
                        Map<String,String> map = new HashMap<String,String>();
                        for(int colNum = xssfRow.getFirstCellNum(); colNum < xssfRow.getLastCellNum(); colNum++ ) {
                            String fieldName = fieldList.get(colNum);
                            String value = getValue(xssfRow.getCell(colNum));
                            map.put(fieldName,value);
                        }
                        list.add(map);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return list;
    }

    @SuppressWarnings("static-access")
    private String getValue(XSSFCell xssfCell) {
        if (xssfCell.getCellType() == xssfCell.CELL_TYPE_BOOLEAN) {
            // 返回布尔类型的值
            return String.valueOf(xssfCell.getBooleanCellValue());
        } else if (xssfCell.getCellType() == xssfCell.CELL_TYPE_NUMERIC) {
            // 返回数值类型的值
            return String.valueOf(xssfCell.getNumericCellValue());
        } else {
            // 返回字符串类型的值
            return String.valueOf(xssfCell.getStringCellValue());
        }
    }
}
